/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

import xowl.gmi.environment.Locale;

/**
 *
 * @author Laurent WOUTERS
 */
public class DataPlaceholder extends Widget {
    public static interface Actions {
        boolean isValid(String data);
        String getValue();
        void setValue(String data);
    }
    
    private Actions actions;
    private Couple minSize;
    private boolean isFill;
    private java.awt.Font font;
    private java.awt.Color fontColor;
    private String defaultEdit;
    private boolean isHovered;
    private String text;
    
    public java.awt.Font getFont() { return font; }
    public void setWidth(int width) { this.minSize = new Couple(width, minSize.y); }
    public void setHeight(int height) { this.minSize = new Couple(minSize.x, height); }
    public void setFontName(String name) { this.font = new java.awt.Font(name, font.getStyle(), font.getSize()); }
    public void setFontStyle(int style) { this.font = new java.awt.Font(font.getFontName(), style, font.getSize()); }
    public void setFontSize(int size) { this.font = new java.awt.Font(font.getFontName(), font.getStyle(), size); }
    public void setFontColor(java.awt.Color color) { this.fontColor = color; }
    
    public DataPlaceholder(Actions actions, Locale locale, int width, int height, boolean isFill, java.awt.Font font, java.awt.Color color, String text) {
        this.actions = actions;
        this.minSize = new Couple(width, height);
        this.isFill = isFill;
        this.font = font;
        this.fontColor = color;
        this.defaultEdit = locale.diagEmptyData;
        this.text = text;
    }
    
    public boolean validateInput(String value) {
        if (actions.isValid(value)) {
            actions.setValue(value);
            return true;
        }
        return false;
    }
    
    public void setValue(String value) { this.text = value; }
    
    @Override public Couple getMinSize() { return minSize; }
    @Override public Couple getMaxSize() { return minSize; }
    @Override public Couple layout() {
        dimension = minSize;
        return minSize;
    }
    @Override public Couple layout(Couple bounds) {
        if (isFill)
            dimension = bounds;
        else
            dimension = minSize;
        return dimension;
    }
    
    @Override public void paint(java.awt.Graphics2D g2d, float alpha, DiagramStyle style, Couple offset) {
        Couple r = position.plus(offset);
        if (isHovered) {
            g2d.setColor(style.editableHoverColor);
            g2d.fillRect((int)r.x, (int)r.y, (int)dimension.x, (int)dimension.y);
        }
        String txt = (text != null)?text:defaultEdit;
        if (font != null)
            g2d.setFont(font);
        else if (this.text != null)
            g2d.setFont(new java.awt.Font(style.fontName, style.fontStyle, style.fontSize));
        else
            g2d.setFont(new java.awt.Font(style.fontName, java.awt.Font.ITALIC, style.fontSize));
        java.awt.FontMetrics fm = g2d.getFontMetrics();
        if (fontColor != null)
            g2d.setColor(getRealColor(fontColor, alpha));
        else
            g2d.setColor(getRealColor(java.awt.Color.BLACK, alpha));
        g2d.setStroke(new java.awt.BasicStroke(1));
        g2d.drawString(txt, (int)(r.x + (dimension.x - fm.stringWidth(txt)) / 2), (int)(r.y + (dimension.y - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent()));
    }
    @Override protected void exportSVG(org.w3c.dom.Document doc, org.w3c.dom.Element parent, float alpha, DiagramStyle style, Couple offset) {
        Couple r = position.plus(offset);
        java.awt.FontMetrics fm = null;
        if (font != null) fm = getFontMetrics(font);
        else fm = getFontMetrics(new java.awt.Font(style.fontName, style.fontStyle, style.fontSize));
        java.awt.Color color = getRealColor(fontColor!=null?fontColor:java.awt.Color.BLACK, alpha);
        String txt = (text != null)?text:defaultEdit;
        double x = r.x + (dimension.x - fm.stringWidth(txt)) / 2;
        double y = r.y + (dimension.y - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent();
        org.w3c.dom.Element elem = doc.createElement("text");
        elem.setAttribute("x", Double.toString(x));
        elem.setAttribute("y", Double.toString(y));
        elem.setAttribute("textLength", Double.toString(fm.stringWidth(txt)));
        elem.setAttribute("font-family", fm.getFont().getFamily());
        elem.setAttribute("font-size", Float.toString(fm.getFont().getSize2D()));
        if ((fm.getFont().getStyle() & java.awt.Font.BOLD) == java.awt.Font.BOLD)
            elem.setAttribute("font-weight", "bold");
        if ((fm.getFont().getStyle() & java.awt.Font.ITALIC) == java.awt.Font.ITALIC)
            elem.setAttribute("font-style", "italic");
        elem.setAttribute("fill", "#" + getColorString(color));
        if (color.getAlpha() != 255) {
            float value = ((float)color.getAlpha()) / 255;
            elem.setAttribute("fill-opacity", Float.toString(value));
        }
        elem.appendChild(doc.createTextNode(txt));
        parent.appendChild(elem);
    }
    
    @Override public boolean onMouseEnter(UserState state, Couple local) {
        if (state != UserState.Normal) return false;
        isHovered = true;
        return true;
    }
    @Override public boolean onMouseLeave() {
        if (!isHovered) return false; 
        isHovered = false;
        return true;
    }
    @Override public boolean onMouseOpenEditor(Diagram diagram) {
        diagram.openEditor(this, text);
        return true;
    }
}
